package com.huiquan.management.dao;

import com.huiquan.analysis.domain.User;
import com.huiquan.analysis.domain.UserPage;
import com.huiquan.framework.base.BaseDao;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserManageDao extends BaseDao {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "USER_MANAGE.";

	@SuppressWarnings("unchecked")
	public List<User> retrieveDisplayList(String realName) {
		Map<String, Object> param = new HashMap<>();
		param.put("hide", 0);
		param.put("realName", realName);

		return (List<User>) this.list(space + "RETRIEVE_LIST", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<User> retrieveHideList(String realName) {
		Map<String, Object> param = new HashMap<>();
		param.put("hide", 1);
		param.put("realName", realName);

		return (List<User>) this.list(space + "RETRIEVE_LIST", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<UserPage> retrieveAllUserPageList() {
		Map<String, Object> param = new HashMap<>();
		return (List<UserPage>) this.list(space + "RETRIEVE_ALL_PAGE_LIST", param, sqlMapClient);
	}

	public UserPage retrievePageByRoleAndTab(User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("role", user.getRole());
		param.put("tabId", user.getTabId());
		return (UserPage) this.object(space + "RETRIEVE_PAGE_BY_ROLE_AND_TAB", param, sqlMapClient);
	}

	public void addUser(User user) {
		this.insert(space + "INSERT_USER", user, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<UserPage> retrieveByRole(String role,String type) {
		Map<String, Object> param = new HashMap<>();
		param.put("role", role);
		param.put("type",type);
		return (List<UserPage>) this.list(space + "RETRIEVE_BY_ROLE", param, sqlMapClient);
	}

	public void setRoleAndOutsourceFlag(String id, String role, int outsourceFlag) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("role", role);
		param.put("outsourceFlag", outsourceFlag);

		this.update(space + "SET_ROLE_AND_OUTSOURCE_FLAG", param, sqlMapClient);
	}

	public void setPage(String id, String tabId) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("tabId", tabId);

		this.update(space + "SET_PAGE", param, sqlMapClient);
	}

	public void setHide(String id, int hideFlag) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("hideFlag", hideFlag);

		this.update(space + "SET_HIDE", param, sqlMapClient);
	}

	public void deleteGroupByUid(String uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("uid", uid);
		this.delete(space + "DELETE_GROUP_BY_UID", param, sqlMapClient);
	}

	public void updateCompany(long id, String companyId) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		if (StringUtils.isBlank(companyId)) {
			companyId = "0";
		}
		param.put("companyId", companyId);

		this.update(space + "UPDATE_COMPANY", param, sqlMapClient);
	}
}
